﻿<%@ WebHandler Language="C#" Class="wxMiniProgramLogin" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;


public class wxMiniProgramLogin : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";



        string openid = context.Request.Headers["X-ACCESS-USER"];//Header中的token

        int aid = 0;

        if (!string.IsNullOrEmpty(context.Request.QueryString["aid"]))
        {
            aid =int.Parse(context.Request.QueryString["aid"]);
        }



        if (string.IsNullOrEmpty(openid))
        {
            HttpContext.Current.Response.Write("parameter error");
            return;
        }




        var msg = "默认收货地址";
        var time = DateTime.Now;

        using (Entities bll = new Entities())
        {
            S_UADDRESS x;

            if (aid == 0)
            {
                x= bll.S_UADDRESS.FirstOrDefault(p => p.Uopenid == openid);
            }
            else
            {

                x= bll.S_UADDRESS.FirstOrDefault(p => p.Uopenid == openid&&p.UAID==aid);

            }


            var addname = new
            {

                province = x.UAProvance,
                city = x.UACity,
                district = x.UADistrict

            };

            var area = new
            {

                title=x.UAddName,
                address= x.UAddress,
                location=x.Ulatng,
                addname

            };


            if (x != null)
            {
                var data = new
                {
                    id = x.UAID,
                    is_default = x.UAState,
                    name = x.UPeopleName,
                    phone = x.UAPhone,
                    province_name = x.UAProvance,
                    city_name = x.UACity,
                    area_name = x.UADistrict,
                    addr = x.UDetailAddress,
                    title=x.UAddName,
                    address=  x.UAddress,
                    area



                };

                context.Response.Write(JsonConvert.SerializeObject(new { code=1, msg, time, data }));

            }

            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new { code=0, msg, time }));
            }



        }

        HttpContext.Current.Response.End();
    }





    public bool IsReusable {
        get {
            return false;
        }
    }

}